package com.totem.base.config;

import com.totem.base.filter.DataReusableFilter;
import com.totem.base.filter.LoginFilter;
import com.totem.base.filter.SecurityFilter;
import io.undertow.UndertowOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;

import com.totem.base.filter.AuthenticationFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
@Configuration
@AutoConfigureAfter({BaseConfiguration.class})
public class WebConfiguration {
    // @Bean
    public UndertowServletWebServerFactory undertowServletWebServerFactory(){
        UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
        factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true));
        log.info("开启http2");
        return factory;
    }

    @Bean
    public WebMvcConfigurer corsConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedMethods("POST","GET","OPTIONS")
                        .allowedHeaders("*")
                        .allowedOrigins("*")
                ;
            }
        };
    }

    @Bean
    public FilterRegistrationBean dataReusableFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new DataReusableFilter());
        registration.addUrlPatterns("/*");
        registration.setName("DataReusableFilter");
        registration.setOrder(0);
        return registration;
    }
    @Bean
    public FilterRegistrationBean securityFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new SecurityFilter());
        registration.addUrlPatterns("/*");
        registration.setName("securityFilter");
        registration.setOrder(1);
        return registration;
    }
    @Bean
    public FilterRegistrationBean loginFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new LoginFilter());
        registration.addUrlPatterns("/*");
        registration.setName("loginFilter");
        registration.setOrder(2);
        return registration;
    }
    @Bean
    public FilterRegistrationBean authenticationFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new AuthenticationFilter());
        registration.addUrlPatterns("/*");
        registration.setName("authenticationFilter");
        registration.setOrder(3);
        return registration;
    }
}
